const crypto = require('crypto');

const baseObj={
	mw:function(map){
		var mw="";
		for(var prop in map){
		    if(map.hasOwnProperty(prop)){
		    	var key=prop;
		    	var val=encodeURI(map[prop]);
		    	if(map[prop]==null||map[prop]=='null'){
		    		val="";
		    	}
		    	mw=mw+key+"="+val+"&";
		    }
		}
		if(mw.length>1){
			mw=mw.substring(0,mw.length-1);
		}
		if(mw!=''){
			mw=encryptByDES(mw);
			mw=encodeURI(mw);
			
		}
		return mw;
	},
	getMd5: function(encryptString){
	    var hasher = crypto.createHash("md5");
	    hasher.update(encryptString);
	    encryptString= hasher.digest('hex');
	    return encryptString;
	    console.log(encryptString);
	},
	openView:function(url){
		mui.openWindow({
			url: url, 
			show:{
				autoShow:false
			}
		});
	}
}
const config = {
    request_prefix: 'http://localhost:10003',
    base_img: 'http://www.baidu.com',
 
}

var desKey="Y74lNq1J";
function encryptByDES(message) {
    var key = new Buffer(desKey);
    var iv = new Buffer(0);
    var cipher = crypto.createCipheriv('des-ecb', key, iv);
    cipher.setAutoPadding(true) //default true
    var ciph = cipher.update(message, 'utf8', 'base64');
    ciph += cipher.final('base64');
    return ciph;
};

// exports.des = {//node.js des加解密

//   algorithm:{ ecb:'des-ecb',cbc:'des-cbc' },
//   encrypt:function(plaintext,iv){
//     var key = new Buffer(key);
//     var iv = new Buffer(iv ? iv : 0);
//     var cipher = crypto.createCipheriv(this.algorithm.ecb, key, iv);
//     cipher.setAutoPadding(true) //default true
//     var ciph = cipher.update(plaintext, 'utf8', 'base64');
//     ciph += cipher.final('base64');
//     return ciph;
//   },
//   decrypt:function(encrypt_text,iv){
//     var key = new Buffer(key);
//     var iv = new Buffer(iv ? iv : 0);
//     var decipher = crypto.createDecipheriv(this.algorithm.ecb, key, iv);
//     decipher.setAutoPadding(true);
//     var txt = decipher.update(encrypt_text, 'base64', 'utf8');
//     txt += decipher.final('utf8');
//     return txt;
//   }

// };

export {baseObj,config}